﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using OpenExpressApp.MetaAttribute;

namespace OpenExpressApp.MetaModel
{
    /// <summary>
    /// 枚举值的逻辑视图
    /// </summary>
    public class EnumViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value">枚举值</param>
        public EnumViewModel(Enum value)
        {
            this.EnumValue = value;

            Type type = value.GetType();
            FieldInfo fieldInfo = type.GetField(value.ToString());
            if (null != fieldInfo)
            {
                var atts = (EnumAttr[])fieldInfo.GetCustomAttributes(typeof(EnumAttr), false);
                if (atts.Length > 0)
                {
                    this.Label = ((EnumAttr)atts[0]).Label;
                }
                else
                {
                    this.Label = value.ToString();
                }
            }
        }

        /// <summary>
        /// 枚举值
        /// </summary>
        public Enum EnumValue { get; set; }

        /// <summary>
        /// 枚举值的显示
        /// </summary>
        public String Label { get; private set; }

        public override bool Equals(object obj)
        {
            return EnumValue.Equals((obj as EnumViewModel).EnumValue);
        }

        public override int GetHashCode()
        {
            return EnumValue.GetHashCode();
        }
    } 
}